package com.amos.generate.service.impl;

import com.amos.core.BaseEntity;
import com.amos.generate.bean.*;
import com.amos.generate.service.AbstractColumnHandler;
import com.amos.generate.service.DataBaseService;
import com.amos.generate.type.TemplateTypeEnum;
import com.amos.generate.util.SpringContext;
import com.amos.generate.util.VelocityUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Copyright © 2018 嘉源锐信. All rights reserved.
 *
 * @Project: mu-exam
 * @ClassName: EntityHandler
 * @Package: com.amos.generate.service.impl
 * @author: zhuqb
 * @Description:
 * @date: 2020/3/11 0011 上午 8:51
 * @Version: V1.0
 */
public class EntityHandler extends AbstractColumnHandler<EntityField> {
    private String path;
    private String rootPackageName;
    private String tableName;

    private List<DataBaseField> list;
    private DataBaseService dataBaseService = SpringContext.getBean(DataBaseService.class);

    public EntityHandler(String path, String rootPackageName, String tableName,List<DataBaseField> list) {
        super(path, rootPackageName, tableName, TemplateTypeEnum.ENTITY,false,list);
        this.path = path;
        this.rootPackageName = rootPackageName;
        this.tableName = tableName;
        this.list = list;
    }

    @Override
    protected Class<?> getFilterClass() {
        return BaseEntity.class;
    }
    /**
     * 获取模板中的属性名
     * @return
     */
    @Override
    public EntityField getColumnList(List<DataBaseField> list) {
        EntityField entityField = new EntityField();
        List<JavaField> fields = new ArrayList<>(list.size());
        List<String> imports = new ArrayList<>();
        list.stream().forEach(item->{
            JavaField javaField  = new JavaField();
            javaField.setJavaField(VelocityUtils.underline2Camel(item.getColumnName()));
            javaField.setJavaType(getJavaType(item));
            fields.add(javaField);
            String importString = this.parseForImport(item);
            if (!StringUtils.isEmpty(importString)) {
                imports.add(importString);
            }
        });
        entityField.setImports(imports);
        entityField.setColumns(fields);
        return entityField;
    }

}
